import config from 'config';
import { NextFunction, Response } from 'express';
import jwt from 'jsonwebtoken';
import { getRepository } from 'typeorm';
import { QyUser as UserEntity } from '@/entity/qy-user.entity';
import HttpException from '@exceptions/HttpException';
import { DataStoredInToken, RequestWithUser } from '@interfaces/auth.interface';

const authMiddleware = async (req: RequestWithUser, res: Response, next: NextFunction) => {
  // try {
  //   const Authorization = req.cookies['Authorization'] || req.header('Authorization').split('Bearer ')[1] || null;
  //
  //   if (Authorization) {
  //     const secretKey: string = config.get('secretKey');
  //     const verificationResponse = (await jwt.verify(Authorization, secretKey)) as DataStoredInToken;
  //     const userId = verificationResponse.id;
  //
  //     const userRepository = getRepository(UserEntity);
  //     const findUser = await userRepository.findOne(userId, { select: ['id', 'email', 'password'] });
  //
  //     if (findUser) {
  //       req.user = findUser;
  //       next();
  //     } else {
  //       next(new HttpException(401, 'Wrong authentication token'));
  //     }
  //   } else {
  //     next(new HttpException(404, 'Authentication token missing'));
  //   }
  // } catch (error) {
  //   next(new HttpException(401, 'Wrong authentication token'));
  // }
};

export default authMiddleware;
